package cn.range.function;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.sql.Connection;

import cn.range.tdomain.BackGroundSMS;
import cn.range.tdomain.DefinedConstants;
import cn.range.tdomain.ForbiddenIP;
import cn.range.tdomain.SuctionPhone;

public class FileClassification {

	/*
	 * Put only the code handling exception problems in catch-clause,
	 * the example in following is not a perfect style.
	 */
	public static void classFile(Document doc, Connection conn, DefinedConstants dc){
		Element root = doc.getDocumentElement();
			
		NodeList nodes = root.getElementsByTagName(dc.gettName2());
		Element element = (Element) nodes.item(0);
			
		try{
			element.getTagName();
			
			/*Dealing with sucphone*/
			SuctionPhone sucPh = new SuctionPhone();
			new HandleNodes().handleSucPhNode(element, sucPh, conn);
		}catch(NullPointerException etName2){
			
			nodes = root.getElementsByTagName(dc.gettName3());
			element = (Element) nodes.item(0);
			
			try{
				element.getTagName();
				
				/*dealing with forbiddip*/
				ForbiddenIP fip = new ForbiddenIP();
				new HandleNodes().handleFipNode(element, fip, conn);
			}catch(NullPointerException etName3){
				
				nodes = root.getElementsByTagName(dc.gettName4());
				element = (Element) nodes.item(0);
				
				try{
					element.getTagName();
					
					/*Dealing with bgsms*/
					BackGroundSMS bgsms = new BackGroundSMS();
					new HandleNodes().handleBgsmsNode(element, bgsms, conn);
				}catch(NullPointerException etName4){
					System.out.println("Not bgsms!");
					System.out.println("It seems some problem of tags has encountered!");
				}
			}
			
		}
			
	}

}
